package org.example.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.example.api.ArticleApi;
import org.example.common.Result;
import org.example.model.Article;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Reference(interfaceClass = ArticleApi.class, version = "1.0.0", check = false)
    private ArticleApi articleApi;


    @GetMapping("/page/user/{userId}")
    @ApiOperation(value = "按照用户id分页查询文章列表")
    public Result<List<Article>> pageArticleByUserId(
            @ApiParam(value = "用户Id", required = true) @PathVariable Long userId,
            @ApiParam(value = "当前页码", required = true) @RequestParam Long page,
            @ApiParam(value = "页数量") @RequestParam(defaultValue = "10") Long size
    ) {
        return articleApi.pageArticleByUserId(userId, page, size);
    }

    @GetMapping("/get")
    @ApiOperation(value = "按照文章id获取整个文章")
    public Result<Article> getArticleById(
            @ApiParam(value = "文章Id", required = true) @RequestParam Long id
    ) {
        System.out.println(8520);
        return articleApi.getArticleById(id);
    }

    @GetMapping("/user/{userId}")
    @ApiOperation(value = "按照用户id查询文章列表")
    public Result<List<Article>> queryArticleByUserId(
            @ApiParam(value = "用户Id", required = true) @PathVariable(value = "userId") Long userId
    ) {
        return articleApi.queryArticleByUserId(userId);
    }

    @GetMapping("/page/user/{userId}/categories/{categoriesId}")
    @ApiOperation(value = "按照用户Id和类别Id分页查询文章列表")
    public Result<List<Article>> pageArticleByUserIdAndCategoriesId(
            @ApiParam(value = "用户Id", required = true) @PathVariable(value = "userId") Long userId,
            @ApiParam(value = "类别Id", required = true) @PathVariable(value = "categoriesId") Long categoriesId,
            @ApiParam(value = "当前页码", required = true) @RequestParam Long page,
            @ApiParam(value = "页数量") @RequestParam(defaultValue = "10") Long size
    ) {
        return articleApi.pageArticleByUserIdAndCategoriesId(userId, categoriesId, page, size);
    }

    @PostMapping("/save")
    public Result<Article> saveArticle(
            @ApiParam(value = "文章信息", required = true) @RequestBody Article article
    ) {
        article.setId(null);
        if (article.getUserId() == null){
            return Result.error("参数未空");
        }
        return articleApi.saveArticle(article);
    }

}
